<?php
/**
 * @author spenceryg
 * @date 2022/2/7
 * @time 7:45 上午
 * @focus love & life
 */

namespace app\admin\controller\sys;

use app\admin\controller\sys\business\AdminUserBusiness;
use app\admin\controller\sys\model\AdminRoleUser;
use app\admin\controller\sys\model\AdminRoleUserModel;
use app\admin\controller\sys\model\AdminUserModel;
use think\App;
use think\facade\Db;
use think\facade\View;
use think\response\Json;

/**
 * 后台用户
 * Class AdminUser
 * @package app\admin\controller\sys
 */
class AdminUser extends \app\BaseController
{
    public $service;

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 列表
     * @return string
     */
    public function index(): string
    {
        View::assign('data', AdminUserModel::getTreeTableList());
        return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_user/index.html');
    }

    /**
     * 添加
     * @return string|Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            AdminUserBusiness::addUser($data);
            return success('添加用户成功');
        } else {
            $roles = Db::name('admin_role')->select();
            View::assign('roleArr', $roles);
            $pid_arr = AdminUserModel::select()->toArray();
            View::assign('pid_arr', $pid_arr);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_user/add.html');
        }
    }

    /**
     * 编辑
     * @return string|Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input('post.');
            AdminUserBusiness::updateUser($data);
            return success('编辑用户成功');
        } else {
            $id = input('id', 0);
            $r = AdminUserModel::find($id);
            $roles = [];
            foreach ($r['adminRole'] as $role) {
                $roles[] = $role['id'];
            }
            View::assign('roles', $roles);
            View::assign('r', $r->getData());
            $roles = Db::name('admin_role')->where('delete_time', '=', null)->select();
            View::assign('roleArr', $roles);
            $pid_arr = AdminUserModel::select()->toArray();
            View::assign('pid_arr', $pid_arr);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_user/update.html');
        }
    }

    /**
     * 删除
     * @return Json
     */
    public function delete(): Json
    {
        $id = input('id', 0);
        AdminUserModel::treeDelete($id);
        AdminRoleUserModel::where('admin_user_id', $id)->delete();
        return success('删除用户成功');
    }

    /**
     * 字段排序
     */
    public function updateSort(): Json
    {
        $id = input("id");
        $sort = input("sort");
        foreach ($id as $key => $vid) {
            $data['id'] = (int)$vid;
            $data['sort'] = (int)$sort[$key];
            AdminUserModel::update($data);
        }
        return success("排序更改成功");
    }
}
